﻿using System.IO;

namespace PageTypeBuilder.SortOrderChecker
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    public static class Extensions
    {
        public static IEnumerable<Type> TypesWithPageTypeAttribute(this IEnumerable<Assembly> assemblies)
        {
            List<Type> types = new List<Type>();

            foreach (Assembly assembly in assemblies)
            {
                try
                {
                    foreach (Type type in assembly.GetTypes())
                    {
                        try
                        {
                            if (type.GetCustomAttributes(false).Any(t => t.GetType().Name == "PageTypeAttribute"))
                                types.Add(type);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return types;
        }

        public static IEnumerable<Type> Concrete(this IEnumerable<Type> types)
        {
            return types.Where(type => !type.IsAbstract);
        }

        public static int GetSortOrder(this Data data)
        {
            int sortOrder = 0;

            PropertyInfo sortOrderProperty = data.Attribute.GetType().GetProperty("SortOrder", BindingFlags.Instance | BindingFlags.Public);

            if (sortOrderProperty != null)
                sortOrder = (int)sortOrderProperty.GetValue(data.Attribute, null);

            if (data.PropertyGroupAttribute != null)
            {
                PropertyInfo startSortOrderFromProperty = data.PropertyGroupAttribute.GetType().GetProperty("StartSortOrderFrom", BindingFlags.Instance | BindingFlags.Public);

                if (startSortOrderFromProperty != null)
                {
                    int startSortOrderFrom = (int)startSortOrderFromProperty.GetValue(data.PropertyGroupAttribute, null);

                    if (startSortOrderFrom > 0)
                        sortOrder += startSortOrderFrom;
                }
            }

            return sortOrder;
        }

        public static int GetSortOrder(this PropertyInfo property)
        {
            int sortOrder = 0;
            object attribute = property.GetCustomAttributes(false).First(p => p.GetType().Name == "PageTypePropertyAttribute");

            PropertyInfo sortOrderProperty = attribute.GetType().GetProperty("SortOrder", BindingFlags.Instance | BindingFlags.Public);

            if (sortOrderProperty != null)
                sortOrder = (int)sortOrderProperty.GetValue(attribute, null);

            return sortOrder;
        }

        public static int GetStartSortOrderFrom(this object obj)
        {
            int startSortOrderFrom = 0;

            PropertyInfo startSortOrderFromProperty = obj.GetType().GetProperty("StartSortOrderFrom", BindingFlags.Instance | BindingFlags.Public);

            if (startSortOrderFromProperty != null)
            {
                startSortOrderFrom = (int)startSortOrderFromProperty.GetValue(obj, null);
            }

            return startSortOrderFrom;
        }

    }
}